package com.example.ar_10.myobj;

import android.graphics.Bitmap;
import android.text.TextUtils;


import com.example.ar_10.base.BitmapUtil;
import com.example.ar_10.base.GLEntity;
import com.example.ar_10.base.GLGroup;
import com.example.ar_10.base.GLObjColorEntity;
import com.example.ar_10.base.MatrixState;
import com.example.ar_10.base.ObjInfo;
import com.example.ar_10.base.ObjLoaderUtil;
import com.example.ar_10.base.GLObjEntity;

import java.util.ArrayList;

/**
 *
 */
public class PipeGroup extends GLGroup {

    private static final String TAG = PipeGroup.class.getSimpleName();


    private ArrayList<ObjInfo> objDatas;
    private ArrayList<GLEntity> mObjSprites = new ArrayList<GLEntity>();

    public PipeGroup(PlaneGlSurfaceView scene) {
        super(scene);
        try {
            objDatas = ObjLoaderUtil.load("box.obj", scene.getResources());
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    ObjInfo data = null;

    public void initObjs() {
        mObjSprites.clear();
        if (objDatas != null) {
            for (int i = 0; i < objDatas.size(); i++) {

                data = objDatas.get(i);

                int diffuseColor = data.mtlData != null ? data.mtlData.Kd_Color : 0xffffffff;
                float alpha = data.mtlData != null ? data.mtlData.alpha : 1.0f;
                String texturePath = data.mtlData != null ? data.mtlData.Kd_Texture : "";


                // 构造对象
                if (data.aTexCoords != null && data.aTexCoords.length != 0 && TextUtils.isEmpty(texturePath) == false) {
                    Bitmap bmp = BitmapUtil.getBitmapFromAsset(getBaseScene().getContext(), texturePath);
                    GLEntity spirit = new GLObjEntity(getBaseScene(), data.aVertices, data.aNormals, data.aTexCoords, alpha, bmp);
                    mObjSprites.add(spirit);
                } else {
                    GLEntity spirit = new GLObjColorEntity(getBaseScene(), data.aVertices, data.aNormals, diffuseColor, alpha);
                    mObjSprites.add(spirit);
                }
            }
        }
    }

    private void init() {
        mSpriteScale = 2;
        // alpha数值
        mSpriteAlpha = 1f;
        // 旋转
        mSpriteAngleX = 90;
        mSpriteAngleY = 0;
        mSpriteAngleZ = 0;
    }


    @Override
    public void onDraw(MatrixState matrixState) {

        super.onDraw(matrixState);

        float[] xyzModel = PositionData.getXyzModel();

        //平移模型到指定位置
        matrixState.translate(xyzModel[0], xyzModel[1], xyzModel[2]);
        matrixState.scale(getSpriteScale(), getSpriteScale(), getSpriteScale());
        matrixState.rotate(getSpriteAngleY(), 0, 1, 0);
        matrixState.rotate(getSpriteAngleX(), 1, 0, 0);
        matrixState.rotate(getSpriteAngleZ(), 0, 0, 1);


        // 绘制
        for (int i = 0; i < mObjSprites.size(); i++) {
            GLEntity sprite = mObjSprites.get(i);
            sprite.onDraw(matrixState);
        }
    }

}